import 'package:cult/function/utils/my_common.dart';
import 'package:flutter/material.dart';
import 'package:ducafe_ui_core/ducafe_ui_core.dart';
import 'package:get/get.dart';
import 'package:google_fonts/google_fonts.dart';
import 'plant_detail_controller.dart';
import '../../../function/model/plant_encyclopedia_model.dart';

/// 植物百科详情页面
class PlantDetailPage extends GetView<PlantDetailController> {
  const PlantDetailPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: const BoxDecoration(
          image: DecorationImage(
            image: AssetImage('assets/images/home_bg.png'),
            fit: BoxFit.fill,
          ),
        ),
        child: SafeArea(
          bottom: false,
          child: Column(
            children: [
              _buildTopNavigation(context),

              Expanded(child: _buildPageContent(context)),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildTopNavigation(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 0.h),
      child: Row(
        children: [
          Icon(
            Icons.arrow_back_ios_new,
            size: 20.sp,
            color: MyCommonUtils.white,
          ).padding(all: 10.w).inkWell(onTap: () => Get.back()),

          SizedBox(width: 16.w),

          // 标题
          Expanded(
            child: Obx(() {
              final plant = controller.plant;
              return Text(
                plant?.plantName ?? 'Plant Detail',
                style: TextStyle(
                  fontSize: 24.sp,
                  fontFamily:
                      GoogleFonts.berkshireSwash(
                        fontWeight: FontWeight.w400,
                      ).fontFamily,
                  color: MyCommonUtils.white,
                ),
                textAlign: TextAlign.center,
              );
            }),
          ),

          SizedBox(width: 56.w),
        ],
      ),
    );
  }

  Widget _buildPageContent(BuildContext context) {
    return Obx(() {
      final plant = controller.plant;

      if (plant == null) {
        return Center(
          child: Text(
            'Plant not found',
            style: TextStyle(fontSize: 16.sp, color: MyCommonUtils.white),
            textAlign: TextAlign.center,
          ),
        );
      }

      return SingleChildScrollView(
        child: SizedBox(
          height: MediaQuery.of(context).size.height + 100.h,
          child: Stack(
            children: [
              Positioned(
                top: 250.h,
                left: 0,
                right: 0,
                child: _buildInfoContainer(context, plant),
              ),

              Positioned(
                top: 60.h,
                left: 0,
                right: 0,
                child: _buildPlantImage(context, plant),
              ),
            ],
          ),
        ),
      );
    });
  }

  Widget _buildInfoContainer(
    BuildContext context,
    PlantEncyclopediaModel plant,
  ) {
    return Container(
      width: double.infinity,
      decoration: const BoxDecoration(
        image: DecorationImage(
          image: AssetImage('assets/images/info_bg.png'),
          fit: BoxFit.fill,
        ),
      ),
      child: Padding(
        padding: EdgeInsets.all(20.w),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(height: 100.h),

            _buildInfoSection('Family & Genus', plant.familyAndGenus),

            SizedBox(height: 16.h),

            _buildInfoSection('Advantages', plant.advantages),

            SizedBox(height: 16.h),

            _buildInfoSection('Cultivation', plant.cultivation),

            SizedBox(height: 16.h),

            _buildInfoSection('Key Features', plant.keyFeatures),

            SizedBox(height: 16.h),

            _buildInfoSection('Growth Habit', plant.growthHabit),

            SizedBox(height: 50.h),
          ],
        ),
      ),
    );
  }

  Widget _buildPlantImage(BuildContext context, PlantEncyclopediaModel plant) {
    return Center(
      child: Container(
        width: 300.w,
        height: 270.w,
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage(plant.plantImage),
            fit: BoxFit.contain,
          ),
        ),
      ),
    );
  }

  Widget _buildInfoSection(String title, String content) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(title)
            .fontSize(20.sp)
            .fontWeight(FontWeight.w700)
            .textColor(MyCommonUtils.white)
            .alignCenter(),

        SizedBox(height: 8.h),

        Container(
          width: double.infinity,
          padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 12.h),
          decoration: BoxDecoration(
            color: Colors.white.withValues(alpha: 0.60),
            borderRadius: BorderRadius.circular(16.r),
          ),
          child: Text(
            content,
            style: TextStyle(
              fontSize: 14.sp,
              color: MyCommonUtils.black,
              fontWeight: FontWeight.w700,
            ),
            textAlign: TextAlign.left,
          ),
        ),
      ],
    );
  }
}
